package com.quzi.kettle.activitise.added;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.quzi.kettle.R;

public class TipVoiceAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<Boolean> ifChecked = new ArrayList<Boolean>();
	private List<String> list_voicename = new ArrayList<String>();

	public Cursor cursor;
	public RingtoneManager rm;

	public int index;
	public boolean firstItemState = true;
	public Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();

	public TipVoiceAdapter(Context context, int index) {
		this.context = context;

		this.index = index;
		if (firstItemState) {
			firstItemState = false;
			if (this.index > -1) {
				map.put(index, true);
			}
		}
		getRing();

	}

	public void getRing() {

		/* 获取RingtoneManager */
		rm = new RingtoneManager(context);
		/* 指定获取类型为短信铃声 */
		rm.setType(RingtoneManager.TYPE_NOTIFICATION);
		/* 创建游标 */
		cursor = rm.getCursor();
		/* 游标移动到第一位，如果有下一项，则添加到ringlist中 */
		if (cursor.moveToFirst()) {
			do { // 游标获取RingtoneManager的列inde x
				list_voicename.add(cursor
						.getString(RingtoneManager.TITLE_COLUMN_INDEX));
			//	cursor.getNotificationUri()
			} while (cursor.moveToNext());
		}
	}

	public void setcheck(int position) {
		// TODO Auto-generated method stub
		if (position == -1) {
			map.clear();
			this.notifyDataSetChanged();
		} else {
			map.clear();
			map.put(position, true);
			this.notifyDataSetChanged();
		}

	}

	// public void updateCheck(ArrayList<Boolean> Checked) {
	// Checked.clear();
	// if (list_voicename == null) {
	// return;
	// }
	// for (int i = 0; i < 10; i++) {
	// Checked.add(false);
	// }
	// }
	//
	// public void update(List<String> voicenamelist) {
	// if (voicenamelist == null) {
	// return;
	// }
	// this.list_voicename = voicenamelist;
	// this.notifyDataSetChanged();
	// }

	// public void setCheck(int positon_before, int position_now) {
	// ifChecked.set(positon_before, false);
	// ifChecked.set(position_now, true);
	// this.notifyDataSetChanged();
	// }

	@Override
	public int getCount() {
		return list_voicename.size();
	}

	@Override
	public Object getItem(int position) {
		return list_voicename.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_tipvoice, null);
			holder = new ViewHolder(convertView);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.iv_item_ifchosen
				.setVisibility(map.get(position) == null ? View.INVISIBLE
						: View.VISIBLE);
		holder.tv_item_voicename.setText(list_voicename.get(position));
		return convertView;
	}

	class ViewHolder {

		TextView tv_item_voicename;
		ImageView iv_item_ifchosen;

		public ViewHolder(View view) {
			tv_item_voicename = (TextView) view
					.findViewById(R.id.tv_item_voicename);
			iv_item_ifchosen = (ImageView) view
					.findViewById(R.id.iv_item_ifchosen);

			view.setTag(this);
		}
	}
}
